/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador;

import Facade.GuardiaFacade;
import Facade.VoluntarioFacade;
import VO.GuardiaVO;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author Jorge
 */
public class GuardiaAction extends DispatchAction {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String REG = "reg";
    private final static String BACK = "back";
    private final static String NEW = "new";
    private final static String HOME = "home";
    private final static String ALL = "all";

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward registrarGuardia(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        Vector listaVol;

        VoluntarioFacade volFcd = new VoluntarioFacade();

        listaVol = volFcd.cargarVoluntarios();


        request.setAttribute("rutVoluntarios", listaVol);



        return mapping.findForward(REG);
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward volver(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(BACK);
    }

    public ActionForward home(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(HOME);

    }

    public ActionForward consultar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        GuardiaFacade fcd = new GuardiaFacade();
        Vector guardias = fcd.cargarGuardias();
        request.setAttribute("guardias", guardias);

        return mapping.findForward(ALL);
    }

    public ActionForward Registrar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String fecha, vols[], dato;
        GuardiaVO vo = new GuardiaVO();
        GuardiaFacade fcd = new GuardiaFacade();
        fecha = request.getParameter("fecha");
        dato = request.getParameter("vol");



        vols = dato.split(",");

        Vector voluntarios = new Vector();
        for (int i = 1; i < vols.length; i++) {
            voluntarios.add(vols[i]);
        }





        vo.setFecha(fecha);
        vo.setVoluntarios(voluntarios);

        if (vo.getFecha().isEmpty() || vo.getVoluntarios().isEmpty()) {
            String aviso = "<span style='color:red;font-weight:bold'>Debe ingresar todos los campos</span>";
            request.setAttribute("aviso", aviso);
        } else {

            if (fcd.registrarGuardia(vo)) {

                String aviso = "<span style='color:green;font-weight:bold'>Registro exitoso</span>";
                request.setAttribute("aviso", aviso);
            }
        }

        Vector listaVol;

        VoluntarioFacade volFcd = new VoluntarioFacade();

        listaVol = volFcd.cargarVoluntarios();


        request.setAttribute("rutVoluntarios", listaVol);


        return mapping.findForward(NEW);
    }
}